﻿<Window
    x:Class="WpfUndoSampleMVVM.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:command="http://www.galasoft.ch/mvvmlight"
    xmlns:local="clr-namespace:WpfUndoSampleMVVM"
    Title="Monitored Undo Framework Sample" Height="350" Width="725"
    Name="LayoutRoot" 
    local:AttachedProperties.RegisterCommandBindings="{Binding CommandBindings}">
    <Window.DataContext>
        <local:MainWindowViewModel></local:MainWindowViewModel>
    </Window.DataContext>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <command:EventToCommand Command="{Binding WindowLoadedCommand}"
                            PassEventArgsToCommand="True" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Grid Background="White" Margin="20">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width=".5*" />
            <ColumnDefinition Width=".5*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        
        <!--
            NOTE:
            Textboxes is bound "Two Way" so that it will pick up changes in the model.
        -->
        
        <Label Grid.Row="0" Grid.Column="0">First Name:</Label>
        <TextBox 
            Name="FirstNameTextbox"
            Grid.Row="0" Grid.Column="1"
            Text="{Binding FirstName, Mode=TwoWay}" 
            HorizontalAlignment="Stretch" Margin="10,2" 
            VerticalAlignment="Top" MinWidth="120"
            IsUndoEnabled="False" 
        />

        <Label Grid.Row="1" Grid.Column="0">Last Name:</Label>
        <TextBox 
            Grid.Row="1" Grid.Column="1"
            Text="{Binding LastName}" 
            HorizontalAlignment="Stretch" Margin="10,2" 
            VerticalAlignment="Top" MinWidth="120" 
            IsUndoEnabled="False" 
        />

        <Label Grid.Row="2" Grid.Column="0">Full Name:</Label>
        <Label 
            Grid.Row="2" Grid.Column="1" Margin="10,2" 
            Content="{Binding ElementName=LayoutRoot, Path=FullName, Mode=OneWay}" />

        <!--
            NOTE:
            Slider is bound "Two Way" so that it will pick up changes in the model.
            
            We capture the Mouse events so that we can "batch" the changes that happen
            between mouse down and mouse up. Otherwise, each incremental movement while
            dragging the mouse will result in a separate undo entry.
        -->
        <Label Grid.Row="3" Grid.Column="0">Age:</Label>
        <Slider 
            Grid.Row="3" Grid.Column="1" Margin="10,2"
            Minimum="0" Maximum="120" 
            TickPlacement="Both" TickFrequency="10"
            Value="{Binding Path=Age, Mode=TwoWay}" 
        >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="PreviewMouseLeftButtonDown">
                    <command:EventToCommand Command="{Binding SliderMouseDownCommand}"
                            PassEventArgsToCommand="True" />
                </i:EventTrigger>
                <i:EventTrigger EventName="LostMouseCapture">
                    <command:EventToCommand Command="{Binding SliderLostMouseCapture}"
                            PassEventArgsToCommand="True" />
                </i:EventTrigger>
            </i:Interaction.Triggers>

        </Slider>

        <TextBlock 
            Grid.Row="4" Grid.Column="1" Margin="20,2" 
        >
            <Run Text="{Binding Path=Age, Mode=OneWay}" />
            <Run Text=" years old" />
        </TextBlock>
        
        <CheckBox
            Grid.Row="5" Grid.Column="1" Margin="20,2"
            Content="Batch changes"
            IsChecked="{Binding Path=BatchAgeChanges, Mode=TwoWay}"
        />

        
        <UniformGrid Rows="1" Grid.Row="7" Grid.ColumnSpan="2" Margin="10">
            <Button Content="Undo (CTL+Z)" Height="23" HorizontalAlignment="Left" Margin="10,2" 
                Name="UndoButton" VerticalAlignment="Top" Width="100" Command="ApplicationCommands.Undo" />

            <Button Content="Redo (CTL+Y)" Height="23" HorizontalAlignment="Left" Margin="10,2" 
                Name="RedoButton" VerticalAlignment="Top" Width="100" Command="Redo" />
        </UniformGrid>

        <Label Grid.Row="0" Grid.Column="2">Undo Stack:</Label>
        <ListBox 
            x:Name="UndoStackListBox"
            Grid.Row="1" Grid.RowSpan="10"
            Grid.Column="2"
            Margin="10"
            ItemsSource="{Binding UndoStack, Mode=OneWay}"
            IsEnabled="True"
            HorizontalAlignment="Stretch"
        >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Description, Mode=OneWay}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <Label Grid.Row="0" Grid.Column="3">Redo Stack:</Label>
        <ListBox 
            x:Name="RedoStackListBox"
            Grid.Row="1" Grid.RowSpan="10"
            Grid.Column="3"
            Margin="10"
            ItemsSource="{Binding Path=RedoStack, Mode=OneWay}"
            IsEnabled="True"
            HorizontalAlignment="Stretch"
        >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Description, Mode=OneWay}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </Grid>

</Window>
